import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/config/ui_constant.dart';
import 'package:star_author/utils/http/http_util.dart';
import 'package:star_author/utils/rouces_util.dart';
import 'package:star_author/widgets/cover_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class OnLineUserDialog extends StatefulWidget {
  const OnLineUserDialog({Key? key}) : super(key: key);

  @override
  State<OnLineUserDialog> createState() => _OnLineUserDialogState();
}

class _OnLineUserDialogState extends State<OnLineUserDialog> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    init();
  }

  init() {
    HttpUtil.commonRequest(
      Get.context!,
      "/api/v1/studios/${StreamConf.liveId}/users",
      queryParameters: {"per_page": 200, "page": 1},
    ).then((value) {
      if (mounted)
        setState(() {
          list = value.data['data'];
        });
    });
  }

  List list = [];

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints:
          BoxConstraints(minHeight: 320.w, maxHeight: Get.height * 0.6),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(12.w),
            topRight: Radius.circular(12.w),
          )),
      padding:
          EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom + 10.w),
      child: Stack(
        children: [
          Positioned(
            child: GestureDetector(
              child: Container(
                alignment: Alignment.topRight,
                child: RoucesUtil.getAssetsIcon('lt_common/clear', 14.w),
                padding: EdgeInsets.only(
                  left: 12.w,
                  right: 12.w,
                  top: 10.w,
                ),
              ),
              onTap: () {
                Get.back();
              },
              behavior: HitTestBehavior.opaque,
            ),
          ),
          Container(
            child: Column(
              children: [
                Container(
                  alignment: Alignment.topCenter,
                  child: Text(
                    "在线观众",
                    style: TextStyle(
                        fontSize: 16.sp,
                        color: Color(0xFF333333),
                        fontWeight: FontWeight.w500),
                  ),
                  margin: EdgeInsets.only(left: 12.w, bottom: 24.w),
                ),
                Container(
                  color: Color(0xFFF6F6F6),
                  padding:
                      EdgeInsets.symmetric(horizontal: 12.w, vertical: 7.w),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        "默认买家等级降序排列",
                        style: TextStyle(
                          fontSize: 12.sp,
                          color: UIConstant.color66,
                        ),
                      ),
                      Text(
                        "${list.length}人在线",
                        style: TextStyle(
                          fontSize: 12.sp,
                          color: UIConstant.color66,
                        ),
                      )
                    ],
                  ),
                ),
                Expanded(
                    child: ListView.builder(
                  itemBuilder: (contex, index) {
                    return Container(
                      margin: EdgeInsets.symmetric(
                          horizontal: 12.w, vertical: 10.w),
                      width: Get.width,
                      child: Row(
                        children: [
                          buildCircleCover(
                              size: 32, cover: list[index]['avatar'] ?? ""),
                          SizedBox(
                            width: 8.w,
                          ),
                          Container(
                            child: Text(
                              "${list[index]['nickname']}",
                              style: TextStyle(
                                  fontSize: 14.sp, color: UIConstant.color33),
                              overflow: TextOverflow.ellipsis,
                            ),
                            constraints: BoxConstraints(
                                minWidth: 1.w, maxWidth: Get.width * 0.6),
                          ),
                          SizedBox(
                            width: 8.w,
                          ),
                          Image.asset(
                            'images/lt_user/owner/level_${list[index]['normal_level']}.png',
                            width: 22.w,
                            height: 12.w,
                            fit: BoxFit.cover,
                          ),
                          SizedBox(
                            width: 8.w,
                          ),
                          Image.asset(
                            'images/lt_user/level/level_${list[index]['bidder_level']}.png',
                            width: 22.w,
                            height: 12.w,
                            fit: BoxFit.cover,
                          ),
                        ],
                      ),
                    );
                  },
                  itemCount: list.length,
                ))
              ],
            ),
            margin: EdgeInsets.only(top: 12.w),
          )
        ],
      ),
    );
  }
}
